/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Event.Controller;

import Entity.Administrator;
import Entity.Contact;
import Entity.Customers;
import Entity.Events;
import Entity.EventsCustomer;
import Entity.Feebacks;
import Models.adaptecAdmin;
import Models.adapterContact;
import Models.adapterCustomers;
import Models.adapterEventCustomer;
import Models.adapterEvents;
import Models.adapterFeedBack;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Administrator
 */
@WebServlet(name = "Controllers", urlPatterns = {"/Controllers"})
public class Controllers extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    int coutEvent;
    int coutEvent2;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(true);
        try {
            adaptecAdmin admin = new adaptecAdmin();
            adapterCustomers customers = new adapterCustomers();
            adapterEvents event = new adapterEvents();
            adapterFeedBack feedBack = new adapterFeedBack();
            adapterEventCustomer eventCus = new adapterEventCustomer();
            SimpleDateFormat format = new SimpleDateFormat("dd/MM");
            String dates = format.format(new Date());
            adapterContact contacts = new adapterContact();
            String action = request.getParameter("btAction");
            if (action.equals("Login")) {
                String userName = request.getParameter("txtUserName");
                String password = request.getParameter("txtPassword");
                boolean bb = customers.kiemTra(userName, password);
                ArrayList<Customers> aCus = new ArrayList<Customers>();
                aCus = customers.searchDobCustomers(dates);
                coutEvent = 0;
                for (Customers c : aCus) {
                    coutEvent++;
                }
                if (bb == true) {
                    if (aCus != null) {
                        request.setAttribute("dob", aCus);
                        request.setAttribute("countdob", coutEvent);
                        session.setAttribute("login", userName);
                        request.getRequestDispatcher("index.jsp").forward(request, response);
                    }
                } else {
                    String erors = "Username or password invalid!";
                    request.setAttribute("erros", erors);
                    request.getRequestDispatcher("LoginForm.jsp").forward(request, response);
                }
            }
            if (action.equals("Search")) {
                String nameAccount = request.getParameter("txtNameAccount");
                if (nameAccount.length() != 0) {

                    ArrayList<Customers> list = new ArrayList<Customers>();
                    list = customers.searchCustomers(nameAccount);
                    request.setAttribute("Search", list);
                    request.getRequestDispatcher("AdminCustomer.jsp").forward(request, response);
                } else {
                    response.sendRedirect("AdminCustomer.jsp");
                }
            }
            if (action.equals("AddCustomer")) {

                String name = request.getParameter("txtNameAccount");
                String pass = request.getParameter("txtPasswourd");
                String fullName = request.getParameter("txtFullname");
                String dob = request.getParameter("txtDob");
                String email = request.getParameter("txtEmail");
                String phone = request.getParameter("txtPhone");
                String address = request.getParameter("txtAddress");
                String gender = request.getParameter("chkGender");
                boolean g = false;
                if (gender != null) {
                    g = true;
                }
                boolean bb = customers.insertCustomers(name, pass, fullName, g, dob, phone, email, address);
                if (bb == true) {
                    response.sendRedirect("AdminCustomer.jsp");
                } else {
                    String lois = "Tài khoản đã tồn tại...Vui chọn tên tài khoản khác! ";
                    request.setAttribute("loi", lois);
                    request.getRequestDispatcher("AdminCustomer.jsp").forward(request, response);
                }

            }
            if (action.equals("UpdaterCustomer")) {
                int code = Integer.parseInt(request.getParameter("txtCode"));
                String name = request.getParameter("txtNameAccount");
                String pass = request.getParameter("txtPasswourd");
                String fullName = request.getParameter("txtFullname");
                String dob = request.getParameter("txtDob");
                String email = request.getParameter("txtEmail");
                String phone = request.getParameter("txtPhone");
                String address = request.getParameter("txtAddress");
                String gender = request.getParameter("chkGender");
                boolean g = false;
                if (gender != null) {
                    g = true;
                }

                customers.updateCustomer(name, pass, fullName, g, dob, phone, email, address, code);
                response.sendRedirect("AdminCustomer.jsp");
            }
            if (action.equals("EditCustomer")) {
                int code = Integer.parseInt(request.getParameter("txtTen"));
                ArrayList<Customers> listTim = new ArrayList<Customers>();
                listTim = customers.searchCodeCustomers(code);
                request.setAttribute("EditCus", listTim);
                request.getRequestDispatcher("AdminCustomer.jsp").forward(request, response);
            }
            if (action.equals("DeleteCustomer")) {
                int codes = Integer.parseInt(request.getParameter("txtTen"));
                boolean b = customers.deleteCustomer(codes);

                if (b == true) {
                    response.sendRedirect("AdminCustomer.jsp");
                } else {
                    String Erros = "Bạn không được phép xóa nha!";
                    request.setAttribute("Erros", Erros);
                    request.getRequestDispatcher("AdminCustomer.jsp").forward(request, response);
                }
            }
            //--------------------------------------------------
            //Event
            if (action.equals("SearchName")) {
                String names = request.getParameter("txtName");
                if (names.length() != 0) {
                    ArrayList<Events> listEvent = new ArrayList<Events>();
                    listEvent = event.searchEvent(names);
                    request.setAttribute("SearchEvent", listEvent);
                    request.getRequestDispatcher("AdminEvents.jsp").forward(request, response);
                } else {
                    response.sendRedirect("AdminEvents.jsp");
                }
            }
            if (action.equals("DeleteEvent")) {
                int codes = Integer.parseInt(request.getParameter("txtCode"));
                boolean b = event.deleteEvent(codes);
                if (b == true) {
                    response.sendRedirect("AdminEvents.jsp");
                } else {
                    String Erros = "Bạn không được phép xóa nha!";
                    request.setAttribute("Erros", Erros);
                    request.getRequestDispatcher("AdminEvents.jsp").forward(request, response);
                }

            }
            if (action.equals("EditEvent")) {
                int codes = Integer.parseInt(request.getParameter("txtCode"));

                ArrayList<Events> listEvent = new ArrayList<Events>();
                listEvent = event.searchCodeEvent(codes);
                request.setAttribute("EditEvent", listEvent);
                request.getRequestDispatcher("AdminEvents.jsp").forward(request, response);
            }
            //----------------------------------------------------
            //Event Customer
            if (action.equals("Finds")) {
                String a = request.getParameter("txtCode");
                if (a.length() != 0) {
                    int code = Integer.parseInt(a);
                    ArrayList<EventsCustomer> listEc = new ArrayList<EventsCustomer>();
                    listEc = eventCus.searchCodeEventCustomers(code);
                    request.setAttribute("EventCus", listEc);
                    request.getRequestDispatcher("AdminEventCustomer.jsp").forward(request, response);
                } else {
                    response.sendRedirect("AdminEventCustomer.jsp");
                }
            }
            if (action.equals("EditCustomerEvent")) {
                int code = Integer.parseInt(request.getParameter("txtCodeEventCus"));
                ArrayList<EventsCustomer> listEc = new ArrayList<EventsCustomer>();
                listEc = eventCus.searchCodeEventCustomers(code);
                request.setAttribute("SearchEventCus", listEc);
                request.getRequestDispatcher("AdminEventCustomer.jsp").forward(request, response);

            }
            if (action.equals("DeleteCustomerEvent")) {
                int code = Integer.parseInt(request.getParameter("txtCodeEventCus"));
                eventCus.deleteEventCustomers(code);
                response.sendRedirect("AdminEventCustomer.jsp");
            }
            if (action.equals("AddEc")) {
                int codeCus = Integer.parseInt(request.getParameter("cobName"));
                int codeEvent = Integer.parseInt(request.getParameter("cobEvent"));
                eventCus.insertEventCustomers(codeCus, codeEvent);
                response.sendRedirect("AdminEventCustomer.jsp");
            }
            if (action.equals("UpdateEc")) {
                int codeEventCustomer = Integer.parseInt(request.getParameter("txtCode"));
                int codeCustomers = Integer.parseInt(request.getParameter("cobName"));
                int codeEvent = Integer.parseInt(request.getParameter("cobEvent"));
                boolean b = eventCus.updateEventCustomers(codeCustomers, codeEvent, codeEventCustomer);
                if (b) {
                    response.sendRedirect("AdminEventCustomer.jsp");
                } else {
                    System.out.println("Loi rui ha ha.............");
                    System.out.println("Hen sui:" + codeCustomers);
                }

            }
            //------------------------------------------------
            //FeedBack
            if (action.equals("Register")) {
                int code = Integer.parseInt(request.getParameter("txtCodes"));
                event.searchCodeEvent(code);
                ArrayList<Events> lstevent = event.searchCodeEvent(code);
                for (Events ev : lstevent) {
                    request.setAttribute("nameevent", ev.getNameEvent());
                }
                String date = request.getParameter("txtDate");
                request.setAttribute("Registers", code);

                request.setAttribute("Date", date);
                request.getRequestDispatcher("RegisterEvent.jsp").forward(request, response);

            }
            if (action.equals("Send")) {
                String name = request.getParameter("txtCustomer");
                int events = Integer.parseInt(request.getParameter("txtEvent"));
                String content = request.getParameter("txtReason");
                String date = request.getParameter("txtDate");
                boolean b = feedBack.insertFeedBack(name, content, date, events);
                if (b == true) {
                    response.sendRedirect("Successfully.jsp");
                }
            }
            if (action.equals("DetailEvent")) {
                String ten = request.getParameter("txtCode");
                if (ten.length() != 0) {
                    int code = Integer.parseInt(ten);
                    ArrayList<Events> listEvent = new ArrayList<Events>();
                    listEvent = event.searchCodeEvent(code);
                    if (listEvent != null) {
                        request.setAttribute("EventDetail", listEvent);
                        request.getRequestDispatcher("Statistic.jsp").forward(request, response);
                    } else {
                        response.sendRedirect("Statistic.jsp");
                    }
                } else {
                    response.sendRedirect("Statistic.jsp");
                }
            }
            if (action.equals("SearchFeed")) {
                String a = request.getParameter("txtCode");
                if (a.length() != 0) {
                    int code = Integer.parseInt(a);
                    ArrayList<Feebacks> f = new ArrayList<Feebacks>();
                    f = feedBack.searchFeedback(code);
                    if (f != null) {
                        request.setAttribute("ff", f);
                        request.getRequestDispatcher("AdminiFeedBack.jsp").forward(request, response);
                    }
                    response.sendRedirect("AdminiFeedBack.jsp");
                } else {
                    response.sendRedirect("AdminiFeedBack.jsp");
                }
            }
            if (action.equals("FindDate")) {
                String start = request.getParameter("txtStart");
                String end = request.getParameter("txtEnd");
                if (start.length() != 0 && end.length() != 0) {
                    ArrayList<Feebacks> listStatistic = new ArrayList<Feebacks>();
                    listStatistic = feedBack.searchDateFeedbackReport(start, end);
                    if (listStatistic != null) {
                        request.setAttribute("Timha", listStatistic);
                        request.getRequestDispatcher("AdminStatistic.jsp").forward(request, response);
                    }
                } else {
                    response.sendRedirect("AdminStatistic.jsp");
                }
            }
            //-------------------------------------------------
            if (action.equals("Details")) {
                String ten = request.getParameter("txtCode");
                if (ten.length() != 0) {
                    int code = Integer.parseInt(ten);
                    ArrayList<Events> listEvent = new ArrayList<Events>();
                    listEvent = event.searchCodeEvent(code);
                    if (listEvent != null) {
                        request.setAttribute("EventHaha", listEvent);
                        request.getRequestDispatcher("AdminStatistic.jsp").forward(request, response);
                    } else {
                        response.sendRedirect("AdminStatistic.jsp");
                    }
                } else {
                    response.sendRedirect("AdminStatistic.jsp");
                }
            }
            //----------------------------------------
            if (action.equals("find")) {
                String start = request.getParameter("txtStart");
                String end = request.getParameter("txtEnd");
                if (start.length() != 0 && end.length() != 0) {
                    ArrayList<Feebacks> listStatistic = new ArrayList<Feebacks>();
                    listStatistic = feedBack.searchDateFeedback(start, end);
                    if (listStatistic != null) {
                        request.setAttribute("Timha", listStatistic);
                        request.getRequestDispatcher("Winners.jsp").forward(request, response);
                    }
                } else {
                    response.sendRedirect("Winners.jsp");
                }
            }
            if (action.equals("findss")) {
                String start = request.getParameter("txtStart");
                String end = request.getParameter("txtEnd");
                if (start.length() != 0 && end.length() != 0) {
                    ArrayList<Feebacks> listStatistic = new ArrayList<Feebacks>();
                    listStatistic = feedBack.searchDateFeedbackReport(start, end);
                    if (listStatistic != null) {
                        request.setAttribute("Timha", listStatistic);
                        request.getRequestDispatcher("Statistic.jsp").forward(request, response);
                    } else {
                        request.setAttribute("Timha", listStatistic);
                        request.getRequestDispatcher("Statistic.jsp").forward(request, response);

                    }
                } else {
                    response.sendRedirect("Statistic.jsp");
                }
            }
            if (action.equals("DetailPage")) {
                String name = request.getParameter("txtTen");
                ArrayList<EventsCustomer> listEc = new ArrayList<EventsCustomer>();
                listEc = eventCus.searchNameCustomers(name);
                request.setAttribute("detail", listEc);
                request.getRequestDispatcher("Winners.jsp").forward(request, response);
            }
            //---------------------------------------
            //Administrator
            if (action.equals("FindName")) {
                String nameAccount = request.getParameter("txtName");
                ArrayList<Administrator> listAdmin = new ArrayList<Administrator>();
                listAdmin = admin.searchAdministrator(nameAccount);
                request.setAttribute("administrator", listAdmin);
                request.getRequestDispatcher("AdminAdministrator.jsp").forward(request, response);
            }

            if (action.equals("EditAdmin")) {
                String name = request.getParameter("txtName");
                ArrayList<Administrator> listAdmin = new ArrayList<Administrator>();
                listAdmin = admin.searchAdministrator(name);
                request.setAttribute("EditHa", listAdmin);
                request.getRequestDispatcher("AdminAdministrator.jsp").forward(request, response);

            }
            if (action.equals("DeleteAdmin")) {
                String name = request.getParameter("txtName");
                admin.deleteAdministrator(name);
                response.sendRedirect("AdminAdministrator.jsp");
            }
            if (action.equals("Inserts")) {
                String name = request.getParameter("txtName");
                String password = request.getParameter("txtPass");
                String fullName = request.getParameter("txtFull");
                String addtrss = request.getParameter("txtAddress");
                boolean b = admin.insertAdministrator(name, password, fullName, addtrss);
                if (b == true) {
                    response.sendRedirect("AdminAdministrator.jsp");
                } else {
                    String erros = "Tài khoản này đã có người dùng vui lòng nhập lại:";
                    request.setAttribute("loi", erros);
                    request.getRequestDispatcher("AdminAdministrator.jsp").forward(request, response);
                }

            }
            if (action.equals("Save")) {
                String name = request.getParameter("txtName");
                String password = request.getParameter("txtPass");
                String fullName = request.getParameter("txtFull");
                String addtrss = request.getParameter("txtAddress");
                admin.updateAdminstrator(password, fullName, addtrss, name);
                response.sendRedirect("AdminAdministrator.jsp");
            }
            if (action.equals("Logins")) {
                String name = request.getParameter("txtUserName");
                String password = request.getParameter("txtPassword");
                boolean b = admin.checkLoginAdministrator(name, password);
                ArrayList<Customers> cc = new ArrayList<Customers>();
                if (b == true) {
                    session.setAttribute("aha", name);
                    cc = customers.searchDobCustomers(dates);
                    coutEvent2 = 0;
                    for (Customers c : cc) {
                        coutEvent2++;
                    }
                    request.setAttribute("countdob2", coutEvent2);
                    request.setAttribute("dob", cc);
                    request.getRequestDispatcher("AdminHome.jsp").forward(request, response);
                }

            }
            if (action.equals("Change")) {
                String name = request.getParameter("txtName");
                String password = request.getParameter("txtPass");
                String fullName = request.getParameter("txtFull");
                String addtrss = request.getParameter("txtAddress");
                admin.updateAdminstrator(password, fullName, addtrss, name);
                response.sendRedirect("AdminAdministrator.jsp");
            }
            //------------------------------------
            //contact
            if (action.equals("AddContact")) {
                String email = request.getParameter("txtEmail");
                String content = request.getParameter("txtContent");
                String name = request.getParameter("txtName");
                contacts.insertContent(email, content, name);
                response.sendRedirect("Successfully.jsp");

            }
            if (action.equals("SearchCode")) {
                String names = request.getParameter("txtCode");

                if (names.length() != 0) {
                    ArrayList<Contact> contact = new ArrayList<Contact>();
                    int code = Integer.parseInt(names);
                    contact = contacts.searchContact(code);
                    if (contact != null) {
                        request.setAttribute("contact", contact);
                        request.getRequestDispatcher("AdminContact.jsp").forward(request, response);
                    }
                    response.sendRedirect("AdminContact.jsp");
                } else {
                    response.sendRedirect("AdminContact.jsp");
                }
            }
            //coder binhnt start
            if (action.equals("ShowUser")) {
//              
                ArrayList<Customers> cus = new ArrayList<Customers>();
                cus = customers.searchDobCustomers(dates);
                String ssu = (String) session.getAttribute("login");
                if (cus != null && ssu != null) {
                    request.setAttribute("userevent", cus);
                }
                request.getRequestDispatcher("index.jsp").forward(request, response);

            }
             if (action.equals("ShowAUser")) {
//
                ArrayList<Customers> cus2 = new ArrayList<Customers>();
                cus2 = customers.searchDobCustomers(dates);
                String ssau = (String) session.getAttribute("aha");
                if (cus2 != null && ssau != null) {
                    request.setAttribute("userevent2", cus2);
                }
                request.getRequestDispatcher("AdminHome.jsp").forward(request, response);

            }

            if (action.equals("ShowUserDetail")) {
//
                ArrayList<Customers> cus = new ArrayList<Customers>();
                int codeUser = Integer.valueOf(request.getParameter("usercode").toString());
                cus = customers.searchCodeCustomers(codeUser);
                String ssu = (String) session.getAttribute("login");
                if (cus != null && ssu != null) {
                    request.setAttribute("userdetail", cus);
                }
                request.getRequestDispatcher("index.jsp").forward(request, response);

            }
            if (action.equals("ShowAUserDetail")) {
//
                ArrayList<Customers> cuas = new ArrayList<Customers>();
                int codeUser = Integer.valueOf(request.getParameter("usercode").toString());
                cuas = customers.searchCodeCustomers(codeUser);
                String ssau = (String) session.getAttribute("aha");
                if (cuas != null && ssau != null) {
                    request.setAttribute("userdetail", cuas);
                }
                request.getRequestDispatcher("AdminHome.jsp").forward(request, response);

            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
